/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AccountSettingsPanel.java
 *
 * Created on Jun 19, 2009, 10:39:40 PM
 */

package ui;

import bus.AccountBUS;
import dto.AccountDTO;
import utils.MsgboxHelper;

/**
 * Add new account
 * @author Hung Cuong <nhc.hcmuns at gmail.com>
 */
public class AccountSettingsPanel extends javax.swing.JFrame {

    AccountDTO acc = new AccountDTO();
    
    /** Creates new form AccountSettingsPanel */
    public AccountSettingsPanel() {
        initComponents();

        getRootPane().setDefaultButton(txtSave);  // set default button
        setLocationRelativeTo(this);            // center the form

        acc = new AccountBUS().getAllRecords().get(0);

        txtUsername.setText(acc.getUsername());
        txtPassword.setText(acc.getPassword());
        txtInHost.setText(acc.getInHost());
        txtOutHost.setText(acc.getOutHost());
        chbUseProxy.setSelected(acc.getProxySet());
        txtProxyHost.setText(acc.getProxyHost());
        txtProxyPort.setText(acc.getProxyPort());

        chbUseProxyActionPerformed(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtInHost = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtOutHost = new javax.swing.JTextField();
        chbUseProxy = new javax.swing.JCheckBox();
        txtProxyHost = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtProxyPort = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Account settings");
        setResizable(false);

        jLabel1.setText("Username:");
        jLabel1.setName("jLabel1"); // NOI18N

        txtUsername.setName("txtUsername"); // NOI18N

        jLabel2.setText("Password:");
        jLabel2.setName("jLabel2"); // NOI18N

        txtPassword.setName("txtPassword"); // NOI18N

        jLabel3.setText("Incoming Host:");
        jLabel3.setName("jLabel3"); // NOI18N

        txtInHost.setName("txtInHost"); // NOI18N

        jLabel4.setText("Outgoing Host:");
        jLabel4.setName("jLabel4"); // NOI18N

        txtOutHost.setName("txtOutHost"); // NOI18N

        chbUseProxy.setText("Use proxy");
        chbUseProxy.setName("chbUseProxy"); // NOI18N
        chbUseProxy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbUseProxyActionPerformed(evt);
            }
        });

        txtProxyHost.setName("txtProxyHost"); // NOI18N

        jLabel5.setText("Proxy Host:");
        jLabel5.setName("jLabel5"); // NOI18N

        txtProxyPort.setName("txtProxyPort"); // NOI18N

        jLabel6.setText("Proxy Port:");
        jLabel6.setName("jLabel6"); // NOI18N

        txtSave.setText("Save");
        txtSave.setName("txtSave"); // NOI18N
        txtSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSaveActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setText("File");
        jMenu1.setName("jMenu1"); // NOI18N
        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 307, Short.MAX_VALUE)
                        .addComponent(txtSave))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtOutHost)
                            .addComponent(txtInHost)
                            .addComponent(txtPassword, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtProxyPort)
                            .addComponent(txtProxyHost, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)))
                    .addComponent(chbUseProxy))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtInHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtOutHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(chbUseProxy)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtProxyHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtProxyPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSave)
                    .addComponent(btnCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chbUseProxyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbUseProxyActionPerformed
        if(chbUseProxy.isSelected()) {
            txtProxyHost.setEnabled(true);
            txtProxyPort.setEnabled(true);
        } else {
            txtProxyHost.setEnabled(false);
            txtProxyPort.setEnabled(false);
        }
    }//GEN-LAST:event_chbUseProxyActionPerformed

    private void txtSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSaveActionPerformed
        try {
            acc.setUsername(txtUsername.getText());
            acc.setPassword(txtPassword.getText());
            acc.setInHost(txtInHost.getText());
            acc.setOutHost(txtOutHost.getText());
            acc.setProxySet(chbUseProxy.isSelected());
            acc.setProxyHost(txtProxyHost.getText());
            acc.setProxyPort(txtProxyPort.getText());

            new AccountBUS().updateAccount(acc);

            MsgboxHelper.inform("Account updated.");
        } catch (Exception ex) {
            MsgboxHelper.showError("Cannot update account.");
        }
        this.dispose();
    }//GEN-LAST:event_txtSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JCheckBox chbUseProxy;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JTextField txtInHost;
    private javax.swing.JTextField txtOutHost;
    private javax.swing.JTextField txtPassword;
    private javax.swing.JTextField txtProxyHost;
    private javax.swing.JTextField txtProxyPort;
    private javax.swing.JButton txtSave;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

}
